package com.xxx.config;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.http.StatViewServlet;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.HashMap;

@Configuration
class DruidConfig {
    @ConfigurationProperties(prefix = "spring.datasource")
    @Bean
    public DataSource druidDataSource(){
        return new DruidDataSource();
    }

    //    后台监控：web.xml
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(),"/druid/*");
//        后台管理员的登陆，账号密码设置
        HashMap<String, String> initParameters = new HashMap<>();
//        增加配置
        //登录的key是固定的 loginUsername
        initParameters.put("loginUsername","root");
        initParameters.put("loginPassword","admin");
//        允许谁可以访问为空这都可
        initParameters.put("allow","");
//        禁止可以访问
//        initParameters.put("gorge","192.168.92.131");
//        设置初始化
        bean.setInitParameters(initParameters);
        return bean;
    }
}
